<template>
  <div class="main_box" style="padding:10px">
    <div class="add_wrap">
      <div class="tit">
        <h2>基础信息</h2>
        <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
      </div>
      <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules" :validate-on-rule-change="false">
        <el-row>
          <el-form-item label="管理员类型：" prop="adminRole">
            <el-select v-model="dataForm.adminRole" placeholder="请选择管理员类型">
              <el-option label="系统管理员" :value="1"></el-option>
              <el-option label="社区管理员" :value="5"></el-option>
              <el-option label="119管理员" :value="6"></el-option>
              <el-option label="120管理员" :value="7"></el-option>
              <el-option label="110管理员" :value="8"></el-option>
              <!--              <el-option label="业务员" :value="10"></el-option>-->
            </el-select>
          </el-form-item>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="dataForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" prop="nickName">
            <el-input v-model="dataForm.nickName"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input type="number" v-model="dataForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" prop="fullName">
            <el-input
              v-model="dataForm.fullName"
              :clearable="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="dataForm.sex">
              <el-radio :label="1">
                男
              </el-radio>
              <el-radio :label="2">
                女
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="所属公司：" prop="sysDepartId" v-show="dataForm.adminRole == 1">
            <el-cascader v-model="dataForm.sysDepartId" :options="departList"
              :props="{ value: 'departId', label: 'departName', children: 'childrens', checkStrictly: true, emitPath: false }"
              clearable filterable>
            </el-cascader>
          </el-form-item>

          <el-form-item label="是否语音提醒" prop="isRemind">
            <el-radio-group v-model="dataForm.isRemind">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="备注：" prop="remark">
            <el-input type="textarea" v-model="dataForm.remark" :clearable="true"></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item>
            <el-button plain type="primary" @click="sureAddClick">保 存</el-button>
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import AuthApi from "@/api/system/auth";
import * as message from "@/constants/message";
import DepartApi from "../../api/system/depart";
import { listSsoUser } from "@/api/system/ssoUser";
import BusinessApi from "@/api/business/business";

export default {
  data() {
    return {
      dataForm: {
        adminId: "",
        userId: "",
        isRemind: 1,
        userName: "",
        nickName: "",
        phone: "",
        fullName: "",
        sex: 1,
        adminRole: "",
        businessId:"",
        sysDepartId: "",
        liveAddress: "",
        remark: "",
      },
      options: [],
      departList: [],
      businessList:[],
      roleRules: {
        adminRole: [
          {required: true, message: "请选择管理员类型", trigger: "change"}
        ],
        businessId: [
          {required: true, message: "请选择商户", trigger: "change"}
        ],
        userName: [
          {required: true, message: "请输人用户名", trigger: "blur"}
        ],
        nickName: [
          {required: true, message: "请输人用户昵称", trigger: "blur"}
        ],
        sex: [
          {required: true, message: "请选择性别", trigger: "change"}
        ],
        isRemind: [
          { required: true, message: "请选择是否语音提醒", trigger: "change" }
        ],
      }
    };
  },
  // },
  beforeRouteLeave(to, from, next) {
    to.meta.keepAlive = true;
    next();
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getData();
      this.getDepartList();
      //this.getBusinessList({businessType:3})
    },
    getData() {
      if (this.$route.query.id) {
        this.dataForm.adminId = this.$route.query.id;
        AuthApi.getAdmin({ adminId: this.$route.query.id }).then(res => {
          this.$Utils.bindingBean(this.dataForm, res.data);

        });
      }
    },
    // 禁止输入空格
    keydown(e) {
      if (e.keyCode == 32) {
        e.returnValue = false;
      }
    },
    // 获取公司列表
    getDepartList() {
      DepartApi.listDeparts().then(res => {
        this.departList = res.data || []
      })
    },
    // 获取商户列表
    /*getBusinessList() {
          BusinessApi.listIdNameVo({businessType:3}).then(res => {
              this.businessList = res.data
          })
      },*/
    sureAddClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(
            message.MSG_CONFIRM_SAVE,
            message.TITLE_PROMPT,
            {}
          ).then(() => {
            if (this.$route.query.id){
              this.editData();
            }else{
              this.addData();
            }
          });
        }
      });
    },
    addData() {
      let data = {
        userName: this.dataForm.userName,
        nickName: this.dataForm.nickName,
        phone: this.dataForm.phone,
        fullName: this.dataForm.fullName,
        sex: this.dataForm.sex,
        adminRole: this.dataForm.adminRole,
        businessId: this.dataForm.businessId,
        sysDepartId:this.dataForm.sysDepartId,
        remark: this.dataForm.remark,
        isRemind: this.dataForm.isRemind,
        liveAddress: this.dataForm.liveAddress
      };
      AuthApi.addAdmin(data).then(data => {
        this.$Utils.notifySuccess();
        this.returnList();
      });
    },
    editData() {
      let data = {
        adminId: this.dataForm.adminId,
        userId: this.dataForm.userId,
        userName: this.dataForm.userName,
        nickName: this.dataForm.nickName,
        phone: this.dataForm.phone,
        fullName: this.dataForm.fullName,
        sex: this.dataForm.sex,
        adminRole: this.dataForm.adminRole,
        businessId: this.dataForm.businessId,
        sysDepartId:this.dataForm.sysDepartId,
        remark: this.dataForm.remark,
        isRemind: this.dataForm.isRemind,
        liveAddress: this.dataForm.liveAddress
      };
      AuthApi.updateAdmin(data).then(data => {
        this.$Utils.notifySuccess();
        this.returnList();
      });
    },
    returnList() {
      this.$store.dispatch("delView", this.$route);
      this.$router.push({ path: "/sys/adminManagement" });
    }

  }
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>
